//
//  WLMNetworkTools.swift
//  WeiBo
//
//  Created by Eric on 2017-01-06.
//  Copyright © 2017 Eric. All rights reserved.
//

import UIKit
import AFNetworking


//枚举
enum HTTPMethod: Int {
    
    case GET
    case POST
}

class WLMNetworkTools: AFHTTPSessionManager {
    
    //单例
    static let sharedManager: WLMNetworkTools = {
        
        let tools = WLMNetworkTools(baseURL: nil)
        
        //设置解析文件类型
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
        
    }()
    
    //封装GET,POST
    func request(method: HTTPMethod, URLString: String, parameters: Any?, finished: @escaping (Any?,Error?) -> ()) {
        let successClosure = { (task: URLSessionDataTask, response: Any?) -> () in
            finished(response,nil)
        }
        let failureClosure = { (task: URLSessionDataTask?, error: Error?) -> () in
            finished(nil,error)
        }
        
        if method == .GET {
            //调用GET
            self.get(URLString, parameters: parameters, progress: nil, success: successClosure, failure: failureClosure)
        }
        else {
            //POST
            self.post(URLString, parameters: parameters, progress: nil, success: successClosure, failure: failureClosure)
        }
        
    }
}
